﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;

namespace BUS
{
    public class CTransferKeysCode
    {        
        /// <summary>
        /// Chuyển từ KeyCode sang Chuỗi
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>         
        public static string SwithText(Keys keys)
        {
            return SwithText((int)keys);
        }

        public static string SwithText(int keys)
        {
            CListKeyCode _ListKeyCode = new CListKeyCode();
            for(int i=0;i<_ListKeyCode.ListKey.Length;i++)
            {
                if(_ListKeyCode.ListKey[i].KeyCode==keys)
                    return _ListKeyCode.ListKey[i].KeyName;
            }
            return "Unknow";
        }

        public static string SwithText(int keys,CListKeyCode ListKeyCode)
        {
            for (int i = 0; i < ListKeyCode.ListKey.Length; i++)
            {
                if (ListKeyCode.ListKey[i].KeyCode == keys)
                    return ListKeyCode.ListKey[i].KeyName;
            }
            return "Unknow";
        }

        public static string[] ListKeyWithString(string KeyName)
        {
            string[] _ListKeyWithString = new string[0];
            CListKeyCode _ListKeyCode = new CListKeyCode();
            for (int i = 0; i < _ListKeyCode.ListKey.Length; i++)
            {
                Boolean _temp =true;
                for (int j = 0; j < KeyName.Length; j++)
                {
                    if (j < _ListKeyCode.ListKey[i].KeyName.Length)
                    {
                        if ((_ListKeyCode.ListKey[i].KeyName[j].ToString().ToUpper() != KeyName[j].ToString().ToUpper()))
                        {
                            _temp = false;
                            break;
                        }
                    }
                    else
                    {
                        _temp = false;
                        break;
                    }
                }
                if (_temp)
                {
                    Array.Resize(ref _ListKeyWithString, _ListKeyWithString.Length + 1);
                    _ListKeyWithString[_ListKeyWithString.Length - 1] = _ListKeyCode.ListKey[i].KeyName;
                }
            }
            return _ListKeyWithString;
        }

        public static string[] ListKeyWithString(string KeyName, CListKeyCode ListKeyCode)
        {
            string[] _ListKeyWithString = new string[0];
            for (int i = 0; i < ListKeyCode.ListKey.Length; i++)
            {
                Boolean _temp = true;
                for (int j = 0; j < KeyName.Length; j++)
                {
                    if (ListKeyCode.ListKey[i].KeyName[j].ToString().ToUpper() != KeyName[j].ToString().ToUpper())
                    {
                        _temp = false;
                        break;
                    }
                }
                if (_temp)
                {
                    Array.Resize(ref _ListKeyWithString, _ListKeyWithString.Length + 1);
                    _ListKeyWithString[_ListKeyWithString.Length - 1] = ListKeyCode.ListKey[i].KeyName;
                }
            }
            return _ListKeyWithString;
        }

        /// <summary>
        /// Chuyển từ chuỗi sang KeyCode
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public static int SwithCode(String keys)
        {
            CListKeyCode _ListKeyCode = new CListKeyCode();
            for(int i=0;i<_ListKeyCode.ListKey.Length;i++)
            {
                if(_ListKeyCode.ListKey[i].KeyName.ToUpper()==keys.ToUpper())
                    return _ListKeyCode.ListKey[i].KeyCode;
            }
            return -1;
        }

        public static int SwithCode(String keys, CListKeyCode ListKeyCode)
        {
            for (int i = 0; i < ListKeyCode.ListKey.Length; i++)
            {
                if (ListKeyCode.ListKey[i].KeyName.ToUpper() == keys.ToUpper())
                    return ListKeyCode.ListKey[i].KeyCode;
            }
            return -1;
        }
    }
}
